﻿using System.IO;
using JSTT_DATA_ENTITY.DBManager;
using JSTT_DATA_ENTITY.XMLDatas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSComm.Utilities.Datas;

namespace JSTT_DATA_ENTITY
{
    /// <summary>
    /// 数据表TF_JSTT_DATA通用接口
    /// </summary>
    public interface I_TF_DEV_DATA
    {
    }

    /// <summary>
    /// JSTT设备的数据通用接口
    /// </summary>
    public interface I_JSTT_DEV_DATA : I_TF_DEV_DATA
    {
        int TD_ID { get;  }
        int JST_DEVICE_ID { get;  }
        string JST_DEVICE_SIM { get;  }
        short JST_EXPT_STATUS { get; }
        short JST_DATATYPE_ID { get;  }
        System.DateTime JST_DATE { get;  }

        byte[] DataBody { get; set; }

        bool FillDatas(JSTT_DataRuleFormats ruleFormat,byte[] databody);


    }

    /// <summary>
    /// JSTT关联所有设备的数据类，是所有设备的父类类型
    /// </summary>
    public class JSTT_DEV_DATA : I_JSTT_DEV_DATA
    {
        private int _td_id;
        private int _jst_device_id;
        private string _jst_device_sim;
        private short _jst_expt_status;
        private short _jst_datatype_id;
        private DateTime _jst_date;
        private byte[] _data_body;

        public int TD_ID
        {
           get { return _td_id; } 
        }

        public int JST_DEVICE_ID
        {
            get { return _jst_device_id; }
        }
 
        public string JST_DEVICE_SIM
        {
           get { return _jst_device_sim; }
        }

        public short JST_EXPT_STATUS
        {
            get { return _jst_expt_status; }
        }

        public short JST_DATATYPE_ID
        {
            get { return _jst_datatype_id; }
        }

        public DateTime JST_DATE
        {
           get { return _jst_date; }
        }

        public byte[] DataBody
        {
            get { return _data_body; }
            set { _data_body = value; }
        }



        /// <summary>
        /// 填充对象的数据
        /// </summary>
        /// <param name="ruleFormat"></param>
        /// <param name="databody"></param>
        /// <returns></returns>
        public virtual bool FillDatas(JSTT_DataRuleFormats ruleFormat, byte[] databody)
        {
            try
            {
                _data_body = databody.CopyBytes(0);
                var o = ruleFormat.JSTT_RuleTableInfo.TableColumns.Where(t => t.ColumnIsKey == "auto");
                if (o == null)
                    return false;
                Dictionary<string, object> autoColumnsProp = new Dictionary<string, object>();
                foreach (DataRuleTableColumn drc in o)
                {
                    DataRuleItem dri = ruleFormat.GetDataRuleItemByColumName(drc.Name);
                    if (dri != null)
                    {
                        //var o1 = ruleFormat.JSTT_RuleDataItems.RuleItems.Where(t => t.ItemProperty.DefineColumnName == drc.Name);
                        byte[] columnDatas = TSComm.Utilities.Datas.ByteHelper.SplitByteDataLH(_data_body,
                                   dri.ByteStart, dri.ByteLength, dri.BitStart, dri.BitLength);

                        autoColumnsProp[drc.Name] = TSComm.Utilities.Datas.ByteHelper.ConvertByteData(columnDatas, drc.ColumnType, drc.TypeLength);
                    }
                }

                this._jst_device_sim = Convert.ToString(autoColumnsProp["JST_DEVICE_SIM"]);
                if (_jst_device_sim.Length > 11 && _jst_device_sim[0] == '0')
                    _jst_device_sim = _jst_device_sim.Substring(1, 11);
                this._jst_device_id = EntityCommon.GetDeviceIDByDTUSIM(_jst_device_sim);

                if (_jst_device_id == 0)  // 无效的DeviceID
                    return false;

                this._td_id = 0;
                this._jst_datatype_id = Convert.ToInt16(autoColumnsProp["JST_DEVTYPE_ID"]);
                this._jst_expt_status = Convert.ToInt16(autoColumnsProp["JST_EXPT_STATUS"]);
                this._jst_date = System.DateTime.Now;
            }
            catch (Exception e)
            {
                JSTTLogger.Logger.ProcWarnLogger(String.Format("Application Error:DevID={0},DevType={1},Error={2}",
                    this.JST_DEVICE_ID,this.JST_DATATYPE_ID,e.Message));
                return false;
            }
            
            return true;
        }
    }



    public enum E_RULETYPE_MAPPING
    {
        JSTT_TYPE_T01 = 1,        // 电压治理装置
        JSTT_TYPE_T02,            // 源补偿和谐波治理装置
        JSTT_TYPE_T03,            // 有源补偿和谐波治理装置
        JSTT_TYPE_T04,            // 综合配电装置
        JSTT_TYPE_T05,            // 环网柜 
        JSTT_TYPE_T06             // 柱上开关 
    }


    /// <summary>
    /// 工厂类，生成具体的Device数据
    /// </summary>
    public static class JSTT_DATA_FACTORY
    {
        /// <summary>
        /// 创建TF_JSTT_DATAS_TXX实体
        /// </summary>
        /// <param name="dataRuleFormat"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static I_JSTT_DEV_DATA CreateJSTTData(TD_DATATYPE_FORMAT_RULES format_rule, byte[] datas)
        {
            JSTT_DataRuleFormats jstt_dataRuleFormat = (JSTT_DataRuleFormats)format_rule.FormatDataRule;
            E_RULETYPE_MAPPING e_ruleType = (E_RULETYPE_MAPPING) format_rule.DF_RULE_TYPE;
            I_JSTT_DEV_DATA dev_data = new JSTT_DEV_DATA();
           
            if(dev_data.FillDatas(format_rule.FormatDataRule, datas))
                return dev_data;
            else
            {
                return null;
            }
        }
    }
}
